package cn.snorlaxzjt.LBSserver.mapper;

import cn.snorlaxzjt.LBSserver.po.entity.Message;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageMapper {
    @Options(useGeneratedKeys = true, keyProperty = "message.id", keyColumn = "id")
    @Insert("INSERT INTO `message` (action,content,sender,receiver,is_visited) " +
            "VALUES (#{message.action},#{message.content},#{message.sender},#{message.receiver},#{visited})")
    void addMessage(@Param("message") Message message, @Param("visited") Integer visited);


    @Select("SELECT action,content,sender,receiver,create_time FROM message WHERE (sender=#{senderId} AND receiver=#{receiverId})OR (sender=#{receiverId} AND receiver=#{senderId})")
    List<Message> getMessage(Integer senderId, Integer receiverId);

    @Select("select * from message where id in(select max(id) from message  where receiver = #{id} " +
            "or sender = #{id} group by receiver,sender) order by create_time desc")
    List<Message> getList(Integer id);
}
